package com.czl.module_service.fragment.assetHandle

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.event.LiveBusCenter
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.adapter.AssetHandleMyApplyAdapter
import com.czl.module_service.databinding.FragmentAssetHandleMyApplyBinding
import com.czl.module_service.extension.loadCompletedData
import com.czl.module_service.viewmodel.AssetHandleMyApplyViewModel

@Route(path = AppConstants.Router.Service.F_SERVICE_ASSET_HANDLE_MY_APPLY)
class AssetHandleMyApplyFragment : BaseFragment<FragmentAssetHandleMyApplyBinding, AssetHandleMyApplyViewModel>() {

    private lateinit var mAdapter: AssetHandleMyApplyAdapter

    private var loaded: Boolean = false

    override fun initContentView(): Int {
        return R.layout.fragment_asset_handle_my_apply
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        super.initData()
        initAdapter()
    }


    override fun onResume() {
        super.onResume()
        loadData()
    }

    private fun loadData() {
        if (!loaded) {
            reload()
        }
    }

    override fun useBaseLayout(): Boolean {
        return false
    }

    private fun initAdapter() {
        mAdapter = AssetHandleMyApplyAdapter(this).apply {
        }
        binding.ryCommon.apply{
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }
    }

    override fun initViewObservable() {
        LiveBusCenter.observeAssetResultReturnEvent(this, {
            reload()
        })
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            this.loadCompletedData(
                data?.hasNextPage,
                viewModel.currentPage == 2,
                data != null,
                data?.total,
                data?.list,
                mAdapter,
                binding.ryCommon,
                binding.smartCommon
            )
        })
        viewModel.uc.loadFinallyEvent.observe(this, {
            loaded = true
        })
    }

    override fun reload() {
        binding.smartCommon.autoRefresh()
    }
}